package dragon.modules.system.user.intercept;

import java.util.List;
import java.util.Map;

import dragon.core.db.DML;
import dragon.core.exception.ExceptionHandler;
import dragon.core.system.po.SysOrg;
import dragon.core.system.po.SysUser;
import dragon.core.util.encrypt.Md5;
import dragon.model.bizObject.BizObject;
import dragon.model.intercept.Interceptor;
import ejp.Database;
import ejw.ServerInterface;

/**
 * 实现功能：ORG截器类
 */
public class UserInterceptor extends Interceptor {

	@Override
	public int beforeAdd(BizObject bizObject, Map<String, String> parameterMap, ServerInterface sif, Database db) throws ExceptionHandler {
		Md5 md5 = new Md5();
		parameterMap.put("PWD", md5.getMD5ofStr(parameterMap.get("PWD").toString()));
		if (parameterMap.get("ORG").equals("") || parameterMap.get("ORG").equals("null")) {
			parameterMap.put("ORG", "0");
		} else {
			String sql = "select IDPATH from " + SysOrg.tableName + " where id=" + parameterMap.get("ORG");
			List<Map<String, Object>> list = DML.query(sql);
			parameterMap.put("OUGROUP", list.get(0).get("IDPATH").toString());
		}
		return super.beforeAdd(bizObject, parameterMap, sif, db);
	}

	@Override
	public int beforeMod(BizObject bizObject, Map<String, String> parameterMap, ServerInterface sif, Database db) throws ExceptionHandler {
		Md5 md5 = new Md5();
		String sql = "select PWD from " + SysUser.tableName + " where id=" + parameterMap.get("ID");
		List<Map<String, Object>> list = DML.query(sql);
		if (parameterMap.get("PWD").trim().equals("")) {
			parameterMap.put("PWD", list.get(0).get("PWD").toString());
		} else {
			parameterMap.put("PWD", md5.getMD5ofStr(parameterMap.get("PWD").toString()));
		}
		return super.beforeAdd(bizObject, parameterMap, sif, db);
	}

}
